/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.entitymanager;

import database.entity.CartItem;
import database.entitymanager.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import database.entity.ShoppingCart;
import database.entity.ShopItem;

/**
 *
 * @author Stefan
 */
public class CartItemManager {

    public CartItemManager() {
        emf = Persistence.createEntityManagerFactory("WebshopITE");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CartItem cartItem) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ShoppingCart shoppingCart = cartItem.getShoppingCart();
            if (shoppingCart != null) {
                shoppingCart = em.getReference(shoppingCart.getClass(), shoppingCart.getCartid());
                cartItem.setShoppingCart(shoppingCart);
            }
            ShopItem shopItem = cartItem.getShopItem();
            if (shopItem != null) {
                shopItem = em.getReference(shopItem.getClass(), shopItem.getItemid());
                cartItem.setShopItem(shopItem);
            }
            em.persist(cartItem);
            if (shoppingCart != null) {
                shoppingCart.getCartItemList().add(cartItem);
                shoppingCart = em.merge(shoppingCart);
            }
            if (shopItem != null) {
                shopItem.getCartItemList().add(cartItem);
                shopItem = em.merge(shopItem);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CartItem cartItem) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CartItem persistentCartItem = em.find(CartItem.class, cartItem.getCartitemid());
            ShoppingCart shoppingCartOld = persistentCartItem.getShoppingCart();
            ShoppingCart shoppingCartNew = cartItem.getShoppingCart();
            ShopItem shopItemOld = persistentCartItem.getShopItem();
            ShopItem shopItemNew = cartItem.getShopItem();
            if (shoppingCartNew != null) {
                shoppingCartNew = em.getReference(shoppingCartNew.getClass(), shoppingCartNew.getCartid());
                cartItem.setShoppingCart(shoppingCartNew);
            }
            if (shopItemNew != null) {
                shopItemNew = em.getReference(shopItemNew.getClass(), shopItemNew.getItemid());
                cartItem.setShopItem(shopItemNew);
            }
            cartItem = em.merge(cartItem);
            if (shoppingCartOld != null && !shoppingCartOld.equals(shoppingCartNew)) {
                shoppingCartOld.getCartItemList().remove(cartItem);
                shoppingCartOld = em.merge(shoppingCartOld);
            }
            if (shoppingCartNew != null && !shoppingCartNew.equals(shoppingCartOld)) {
                shoppingCartNew.getCartItemList().add(cartItem);
                shoppingCartNew = em.merge(shoppingCartNew);
            }
            if (shopItemOld != null && !shopItemOld.equals(shopItemNew)) {
                shopItemOld.getCartItemList().remove(cartItem);
                shopItemOld = em.merge(shopItemOld);
            }
            if (shopItemNew != null && !shopItemNew.equals(shopItemOld)) {
                shopItemNew.getCartItemList().add(cartItem);
                shopItemNew = em.merge(shopItemNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cartItem.getCartitemid();
                if (findCartItem(id) == null) {
                    throw new NonexistentEntityException("The cartItem with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CartItem cartItem;
            try {
                cartItem = em.getReference(CartItem.class, id);
                cartItem.getCartitemid();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cartItem with id " + id + " no longer exists.", enfe);
            }
            ShoppingCart shoppingCart = cartItem.getShoppingCart();
            if (shoppingCart != null) {
                shoppingCart.getCartItemList().remove(cartItem);
                shoppingCart = em.merge(shoppingCart);
            }
            ShopItem shopItem = cartItem.getShopItem();
            if (shopItem != null) {
                shopItem.getCartItemList().remove(cartItem);
                shopItem = em.merge(shopItem);
            }
            em.remove(cartItem);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CartItem> findCartItemEntities() {
        return findCartItemEntities(true, -1, -1);
    }

    public List<CartItem> findCartItemEntities(int maxResults, int firstResult) {
        return findCartItemEntities(false, maxResults, firstResult);
    }

    private List<CartItem> findCartItemEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CartItem.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CartItem findCartItem(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CartItem.class, id);
        } finally {
            em.close();
        }
    }

    public int getCartItemCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CartItem> rt = cq.from(CartItem.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
